package dev.huidocs.docs.rest;

import com.gitee.coadmin.annotation.UnifiedAPI;
import com.gitee.coadmin.exception.CoException;
import com.gitee.coadmin.utils.ExcelUtils;
import com.gitee.coadmin.base.PageInfo;
import com.gitee.coadmin.modules.logging.annotation.Log;
import com.gitee.coadmin.modules.logging.annotation.type.LogActionType;
import dev.huidocs.docs.service.DocResHistoryService;
import dev.huidocs.docs.service.dto.DocResHistoryDTO;
import dev.huidocs.docs.service.dto.DocResHistoryQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/*  添加菜单的 SQL
*/

/**
 * @author jinjin
 * @since 2024-10-11
 **/
@UnifiedAPI
@RestController
@RequiredArgsConstructor
@Api(tags = "Resource history")
@RequestMapping("/api/docs/doc-res-history")
public class DocResHistoryController {

    private final DocResHistoryService docResHistoryService;

    @GetMapping
    @Log("查询Resource history")
    @ApiOperation("查询Resource history")
    @PreAuthorize("@el.check('docResHistory:list')")
    public PageInfo<DocResHistoryDTO> query(DocResHistoryQueryParam query, Pageable pageable){
        return docResHistoryService.pageByParam(query,pageable);
    }

    @PostMapping
    @Log(value = "新增Resource history", type = LogActionType.ADD)
    @ApiOperation("新增Resource history")
    @PreAuthorize("@el.check('docResHistory:add')")
    public void create(@Validated @RequestBody DocResHistoryDTO res){
        docResHistoryService.insert(res);
    }

    @PutMapping
    @Log(value = "修改Resource history", type = LogActionType.UPDATE)
    @ApiOperation("修改Resource history")
    @PreAuthorize("@el.check('docResHistory:edit')")
    public void update(@Validated @RequestBody DocResHistoryDTO res){
        docResHistoryService.updateById(res);
    }

    @DeleteMapping
    @Log(value = "删除Resource history", type = LogActionType.DELETE)
    @ApiOperation("删除Resource history")
    @PreAuthorize("@el.check('docResHistory:del')")
    public void delete(@RequestBody Set<Long> ids) {
        docResHistoryService.removeByIds(ids);
    }

    @Log("导出Resource history")
    @ApiOperation("导出Resource history")
    @UnifiedAPI(enable = false)
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('docResHistory:down')")
    public void download(DocResHistoryQueryParam criteria, HttpServletResponse response) {
        try {
            List<DocResHistoryDTO> dtos = docResHistoryService.listByParam(criteria);
            ExcelUtils.exportExcel(dtos, null, "导出Resource history", DocResHistoryDTO.class, "", response);
        } catch (IOException e) {
            throw new CoException("导出失败");
        }
    }
}
